<?php

namespace App\Models;



class MallOrderProduct extends Base
{
    const STATUS_WAIT_PAY = 0;
    const STATUS_WAIT_PRODUCT = 1;
    const STATUS_DELIVER_PRODUCT = 2;
    const STATUS_FINISH = 3;
    const STATUS_CANCEL = 4;
    const STATUS_REFUND_FINISH = 5;

    const PAY_TYPE_WX = 2;
    const PAY_TYPE_ALI = 1;

    const CLOSE_TYPE_USER = 1;
    const CLOSE_TYPE_ADMIN = 2;
    const CLOSE_TYPE_SYSTEM = 3;


    protected $appends = [ 'paid_amount_yuan', 'total_amount'];

    protected $casts = [
        'specs_info' => 'array',
        'specs_info_show' => 'array',
    ];


    public function product() {
        return $this->belongsTo(MallOrderProductSnapshot::class, 'product_snapshot_id');
    }

    public function address() {
        return $this->belongsTo(AddressSnapshot::class, 'address_snapshot_id');
    }

    public function express() {
        return $this->belongsTo(ExpressType::class, 'express_id');
    }

    public function getPaidAmountYuanAttribute() {
        return format_yuan($this->paid_amount);
    }

    public function setUnitAmountAttribute($val) {
        $this->attributes['unit_amount'] = $val * 100;
    }

    public function getUnitAmountAttribute($val) {
        return format_yuan($val);
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function setSpecsDetailAttribute($val) {
        $this->attributes['specs_detail'] = $val;
    }

    public function setSpecsDetailInfoAttribute($val) {
        $this->attributes['specs_detail_info'] = $val;
    }

    public function order() {
        return $this->belongsTo(MallOrder::class, 'order_id')->withTrashed();
    }

    public function getAssignFareAttribute($val) {
        return format_yuan($val);
    }

    public function getTotalAmountAttribute() {
        return $this->assign_fare + $this->paid_amount_yuan;
    }
}
